// ReadOnlyDictionary.cs
// 
// This code is trivial, and considered ineligible for copyright. As such,
// it is considered to be in the public domain.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LibFreeZzaph
{
	
	/// <summary>
	/// A read-only Dictionary wrapper. Since many of the methods in the plug-in
	/// architecture returns dictionaries that should not be modified, and since
	/// there is no built-in ReadOnlyDictionary, (only ReadOnlyCollection) I
	/// felt that it was necessary to create one.
	/// </summary>
	public sealed class ReadOnlyDictionary<TKey,TValue> : IDictionary<TKey,TValue>
	{
		private IDictionary<TKey,TValue> wrappedDictionary;
		
		public ReadOnlyDictionary(IDictionary<TKey,TValue> dictionary)
		{
			this.wrappedDictionary = dictionary;
		}
		
		public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
		{
		    foreach (KeyValuePair<TKey, TValue> item in wrappedDictionary)
		    {
		        yield return item;
		    }
		}
		
		IEnumerator IEnumerable.GetEnumerator()
		{
		    return GetEnumerator();
		}
		
		public bool ContainsKey(TKey key)
		{
		    return wrappedDictionary.ContainsKey(key);
		}


		public bool TryGetValue(TKey key, out TValue value)
		{
		    return wrappedDictionary.TryGetValue(key, out value);
		}


		public bool Contains(KeyValuePair<TKey, TValue> item)
		{
		    return wrappedDictionary.Contains(item);
		}


		public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
		{
		    wrappedDictionary.CopyTo(array, arrayIndex);
		}
		
		public int Count
		{
		    get
		    {
		        return wrappedDictionary.Count;
		    }
		}
		
		public bool IsReadOnly
		{
		    get { return true; }
		}
		
		public TValue this[TKey key]
		{
		    get
		    {
		        return wrappedDictionary[key];
		    }
		}
		
		TValue IDictionary<TKey, TValue>.this[TKey key]
		{
		    get
		    {
		        return this[key];
		    }
		    set
		    {
		        throw new NotSupportedException();
		    }
		}
		
		public ICollection<TKey> Keys
		{
		    get
		    {
		        ReadOnlyCollection<TKey> keys =
		            new ReadOnlyCollection<TKey>(new List<TKey>(wrappedDictionary.Keys));
		        return keys;
		    }
		}


		public ICollection<TValue> Values
		{
		    get
		    {
		        ReadOnlyCollection<TValue> values =
		            new ReadOnlyCollection<TValue>(new List<TValue>(wrappedDictionary.Values));
		        return values;
		    }
		}
		
		void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
		{
		    throw new NotSupportedException();
		}


		bool IDictionary<TKey, TValue>.Remove(TKey key)
		{
		    throw new NotSupportedException();
		}


		void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
		{
		    throw new NotSupportedException();
		}


		bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
		{
		    throw new NotSupportedException();
		}


		void ICollection<KeyValuePair<TKey, TValue>>.Clear()
		{
		    throw new NotSupportedException();
		}
	}
}